import axios from 'axios'
import api from "@/assets/js/api.js"
import cookies from 'vue-cookies'
import { Message } from 'element-ui';
import { Notification } from 'element-ui';
const gettoken = function() {
    if (cookies.get("URL") != null && cookies.get("RTOKEN") != null) {
        return cookies.get("RTOKEN")
    }
    return cookies.get("token");
}
const getUrl = function() {
    if (cookies.get("URL") != null && cookies.get("RTOKEN") != null) {
        return cookies.get("URL")
    }
    return api.httpUrl;
}
const getMyhost = function() {
    return getUrl()
}
export default {
    getMyhost,
    Post: (url, json, call, text) => {
        let local_notification = Notification.info({
            title: text,
            dangerouslyUseHTMLString: true,
            message: '<i class="el-icon-loading"></i> 正在执行',
            duration: 0
        })
        var instance = axios.create({
            baseURL: getUrl(),
        })
        instance.defaults.headers.common["Token"] = gettoken()
        instance.post(url, json).then((response) => {
            Notification.success({
                title: text,
                dangerouslyUseHTMLString: true,
                message: '执行成功'
            })
            call(response.data.result)
            local_notification.close()
        }).catch((error) => {
            if (error.response.status == 401 || error.response.status == 404) {
                setTimeout(() => {
                    window.location.href = "#/login"
                }, 2000);
            }
            Notification.error({
                title: text,
                dangerouslyUseHTMLString: true,
                message: error.response.data.msg
            })
            local_notification.close()
        })
    },
    Get: (url, json, call) => {
        console.log(getUrl())
        var instance = axios.create({
            baseURL: getUrl(),
        })
        instance.defaults.headers.common["Token"] = gettoken()
        instance.get(url, { params: json }).then((response) => {
            call(response.data.result)
            return true
        }).catch((error) => {
            if (error.response.status == 401 || error.response.status == 404) {
                setTimeout(() => {
                    window.location.href = "#/login"
                }, 2000);
            }
            Message.error(error.response.data.msg)
        })
    },
    Put: (url, json, call, text) => {
        let local_notfication = Notification.info({
            title: text,
            dangerouslyUseHTMLString: true,
            message: '<i class="el-icon-loading"></i>  正在执行',
            duration: 0
        })
        var instance = axios.create({
            baseURL: getUrl(),
        })
        instance.defaults.headers.common["Token"] = gettoken()
        instance.put(url, json).then((response) => {
            call(response.data.result)
            Notification.success({
                title: text,
                dangerouslyUseHTMLString: true,
                message: "成功"
            })
            local_notfication.close()
        }).catch((error) => {
            if (error.response.status == 401 || error.response.status == 404) {
                setTimeout(() => {
                    window.location.href = "#/login"
                }, 2000);
            }
            Notification.error({
                title: text,
                dangerouslyUseHTMLString: true,
                message: "失败:" + error.response.data.msg
            })
            local_notfication.close()
        })

    },
    Delete: (url, json, call, text) => {
        let local_notfication = Notification.info({
            title: text,
            dangerouslyUseHTMLString: true,
            message: '<i class="el-icon-loading"></i>  正在执行',
            duration: 0
        })
        var instance = axios.create({
            baseURL: getUrl(),
        })
        instance.defaults.headers.common["Token"] = gettoken()
        instance.delete(url, { params: json }).then((response) => {
            call(response.data.result)
            Notification.success({
                title: text,
                dangerouslyUseHTMLString: true,
                message: "成功"
            })
            local_notfication.close()
        }).catch((error) => {
            if (error.response.status == 401 || error.response.status == 404) {
                setTimeout(() => {
                    window.location.href = "#/login"
                }, 2000);
            }
            Notification.error({
                title: text,
                dangerouslyUseHTMLString: true,
                message: "失败:" + error.response.data.msg
            })
            local_notfication.close()
        })
    }
}